package com.atguigu.springcloud.cloudsentinelgatewaydevelop.service.impl;

import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.DO.SentinelParamFLowRuleDO;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.dto.SentinelParamFlowRuleDTO;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.constants.TableNameConstants;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.mapper.ParamFlowRuleMapper;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.service.ParamFlowRuleService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.r2dbc.spi.ConnectionFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.r2dbc.core.DatabaseClient;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;

import static org.springframework.data.r2dbc.query.Criteria.where;

/**
 * xx
 *
 * @author BugFree Jin
 * @version 1.0
 * @since 2022/3/3 10:07
 */
@Service
@RequiredArgsConstructor
public class ParamFlowRuleServiceImpl implements ParamFlowRuleService {

    private final ParamFlowRuleMapper paramFlowRuleMapper;
    private final ConnectionFactory connectionFactory;
    private DatabaseClient client = null;

    @PostConstruct
    void init() {
        this.client = DatabaseClient.create(connectionFactory);
    }

    private SentinelParamFLowRuleDO copyField(SentinelParamFlowRuleDTO sentinelParamFlowRuleDTO) {
        SentinelParamFLowRuleDO sentinelParamFLowRuleDO = new SentinelParamFLowRuleDO();
        SentinelParamFLowRuleDO.ParamFlowRule paramFlowRule = new SentinelParamFLowRuleDO.ParamFlowRule();
        BeanUtils.copyProperties(sentinelParamFlowRuleDTO, sentinelParamFLowRuleDO, "param_flow_rule");
        BeanUtils.copyProperties(sentinelParamFlowRuleDTO, paramFlowRule);
        String paramFlowRuleStr = "";
        try {
            paramFlowRuleStr = new ObjectMapper().writeValueAsString(paramFlowRule);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        sentinelParamFLowRuleDO.setParamFlowRule(paramFlowRuleStr);
        return sentinelParamFLowRuleDO;
    }

    @Override
    public Mono<Integer> save(SentinelParamFlowRuleDTO sentinelParamFlowRuleDTO) {
        SentinelParamFLowRuleDO sentinelParamFLowRuleDO = copyField(sentinelParamFlowRuleDTO);
        return client.insert()
                .into(TableNameConstants.PARAMFLOWRULE)
                .nullValue("id", Long.class)
                .value("application_name", sentinelParamFLowRuleDO.getApplicationName())
                .value("resource", sentinelParamFLowRuleDO.getResource())
                .value("param_flow_rule", sentinelParamFLowRuleDO.getParamFlowRule())
                .fetch()
                .rowsUpdated();
    }

    @Override
    public Mono<Integer> deleteById(Long id) {
        return paramFlowRuleMapper.deleteByID(id);
    }

    @Override
    public Mono<Integer> deleteByAyApplicationName(String applicationName) {
        return paramFlowRuleMapper.deleteByAyApplicationName(applicationName);
    }

    @Override
    public Mono<Integer> updateById(SentinelParamFlowRuleDTO sentinelParamFlowRuleDTO, Long id) {
        SentinelParamFLowRuleDO sentinelParamFLowRuleDO = copyField(sentinelParamFlowRuleDTO);
        return client.update()
                .table(SentinelParamFLowRuleDO.class)
                .using(sentinelParamFLowRuleDO)
                .matching(where("id").is(id))
                .fetch()
                .rowsUpdated();
    }

    @Override
    public Mono<SentinelParamFlowRuleDTO> findById(Long id) {
        return client.select()
                .from(TableNameConstants.PARAMFLOWRULE)
                .matching(where("id").is(id))
                .as(SentinelParamFLowRuleDO.class)
                .fetch()
                .one()
                .map(SentinelParamFLowRuleDO::getSentinelParamFlowRuleDTO)
                .log();
    }

    @Override
    public Flux<SentinelParamFlowRuleDTO> findByApplicationName(String applicationName) {
        return client.select()
                .from(TableNameConstants.PARAMFLOWRULE)
                .matching(where("application_name").is(applicationName))
                .as(SentinelParamFLowRuleDO.class)
                .fetch()
                .all()
                .map(SentinelParamFLowRuleDO::getSentinelParamFlowRuleDTO)
                .log();
    }
}
